package edu.westga.cs.recommender.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import edu.westga.cs.recommender.model.datatypes.*;
import edu.westga.cs.recommender.controller.MainController;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.MutableComboBoxModel;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class SearchOrProfileDemo extends javax.swing.JFrame implements ActionListener {
	//Fields
	private JTextArea userTextArea;
	private JTextField tagTextField3;
	private JTextArea itemTextArea;
	private JComboBox itemComboBox;
	private JTextArea profileTextArea;
	private JButton searchCommitButton;
	private JTextField tagTextField2;
	private JTextField tagTextField1;
	private JComboBox profileComboBox;

	//Fields - Brent
	private MainController mc;
	private String currentProfile;
	private String currentItem;
	private LinkedList<Item> matchingItems;
	private LinkedList<String> searchTags;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				SearchOrProfileDemo inst = new SearchOrProfileDemo();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	//Controller
	public SearchOrProfileDemo() {
		super();
		this.mc = new MainController();
		initGUI();
	}
	
	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout((JComponent)getContentPane());
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				userTextArea = new JTextArea();
				userTextArea.setText("User");
			}
			{
				MutableComboBoxModel profileComboBoxModel = 
					new DefaultComboBoxModel(
							new String[] { "Item One" });
				profileComboBox = new JComboBox();
				profileComboBox.setModel(profileComboBoxModel);
				profileComboBox.addActionListener(this);
				profileComboBox.setActionCommand("profile");
			}
			{
				profileTextArea = new JTextArea();
			}
			{
				MutableComboBoxModel itemComboBoxModel = 
					new DefaultComboBoxModel(
							new String[] { "Item One" });
				itemComboBox = new JComboBox();
				itemComboBox.setModel(itemComboBoxModel);
				itemComboBox.addActionListener(this);
				itemComboBox.setActionCommand("item");
			}
			{
				itemTextArea = new JTextArea();
				itemTextArea.setWrapStyleWord(true);
				itemTextArea.setLineWrap(true);
			}
			{
				tagTextField1 = new JTextField();
				tagTextField1.setText("");
			}
			{
				tagTextField2 = new JTextField();
				tagTextField2.setText("");
			}
			{
				tagTextField3 = new JTextField();
				tagTextField3.setText("");
			}
			{
				searchCommitButton = new JButton();
				searchCommitButton.setText("Go");
				searchCommitButton.addActionListener(this);
				searchCommitButton.setActionCommand("go");
			}
			thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addComponent(userTextArea, GroupLayout.PREFERRED_SIZE, 53, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(profileComboBox, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addGroup(thisLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
				    .addComponent(tagTextField1, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				    .addComponent(tagTextField2, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				    .addComponent(tagTextField3, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				    .addComponent(searchCommitButton, GroupLayout.Alignment.BASELINE, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE))
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(profileTextArea, GroupLayout.PREFERRED_SIZE, 42, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0, GroupLayout.PREFERRED_SIZE)
				.addComponent(itemComboBox, GroupLayout.PREFERRED_SIZE, 26, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				.addComponent(itemTextArea, 0, 194, Short.MAX_VALUE)
				.addContainerGap());
			thisLayout.setHorizontalGroup(thisLayout.createSequentialGroup()
				.addContainerGap()
				.addGroup(thisLayout.createParallelGroup()
				    .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				        .addGroup(thisLayout.createParallelGroup()
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(tagTextField1, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
				                .addComponent(tagTextField2, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(profileComboBox, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
				                .addGap(26))
				            .addGroup(GroupLayout.Alignment.LEADING, thisLayout.createSequentialGroup()
				                .addComponent(itemComboBox, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)
				                .addGap(26)))
				        .addComponent(tagTextField3, 0, 93, Short.MAX_VALUE)
				        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
				        .addComponent(searchCommitButton, GroupLayout.PREFERRED_SIZE, 69, GroupLayout.PREFERRED_SIZE)
				        .addGap(6))
				    .addComponent(userTextArea, GroupLayout.Alignment.LEADING, 0, 360, Short.MAX_VALUE)
				    .addComponent(profileTextArea, GroupLayout.Alignment.LEADING, 0, 360, Short.MAX_VALUE)
				    .addComponent(itemTextArea, GroupLayout.Alignment.LEADING, 0, 360, Short.MAX_VALUE))
				.addContainerGap());
			pack();
			this.getUserInfo();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//Methods - Brent
	/**
	 * Action listener; passes off to helper methods to handle different events.
	 */
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "profile") {
			if (this.profileComboBox.getModel().getSize() != 0) {
				this.currentProfile = this.profileComboBox.getModel().getSelectedItem().toString();
				this.getProfileInfo("userTest", this.currentProfile);
			}
		}
		if (e.getActionCommand() == "item") {
			if (this.itemComboBox.getModel().getSize() != 0) {
				this.currentItem = this.itemComboBox.getModel().getSelectedItem().toString();
				this.setItemInfo(this.currentItem);
			}
		}
		if (e.getActionCommand() == "go") {
			this.runSearch();
		}
		
	}
	
	/**
	 * Retrieves user info - currently hard coded to call the test user.
	 */
	private void getUserInfo() {
		String newline = System.getProperty("line.separator");
		User currentUser = this.mc.getUser("userTest");
		String display = "User Login: userTest" + newline +
			"User Name: " + currentUser.getName() + newline +
			"Profiles: ";
		int num = currentUser.getProfiles().size();
		String profileName;
		this.profileComboBox.removeAllItems();
		for (int n = 0; n < num; n++) {
			display = display + currentUser.getProfiles().get(n) + ", ";
			profileName = currentUser.getProfiles().get(n);
			this.profileComboBox.addItem(profileName);
		}
		int endPoint = display.length() - 2;
		display = display.substring(0, endPoint);
		this.userTextArea.setText(display);
		String firstProfile = currentUser.getProfiles().get(0);
		this.getProfileInfo("userTest", firstProfile);
	}
	
	/**
	 * Retrieves information for a saved search profile.
	 * @param login Login name for the user.
	 * @param profile Profile name for the search profile.
	 */
	private void getProfileInfo(String login, String profile) {
		String newline = System.getProperty("line.separator");
		Profile currentProfile = this.mc.getProfile(login, profile);
		String display = "Profile Name: " + profile + newline +
			"Tags: ";
		int num = currentProfile.getTags().size();
		String tag;
		for (int n = 0; n < num; n++) {
			tag = currentProfile.getTags().get(n);
			display = display + tag + ", ";
		}
		int endPoint = display.length() - 2;
		display = display.substring(0, endPoint);
		profileTextArea.setText(display);
		this.matchingItems = this.mc.getItems(currentProfile);
		String itemName;
		num = this.matchingItems.size();
		this.itemComboBox.removeAllItems();
		for (int m = 0; m < num; m++) {
			itemName = this.matchingItems.get(m).getName();
			this.itemComboBox.addItem(itemName);
		}
	}
	
	/**
	 * Sets the item information for a specific item.
	 * @param itemName The name for the item to set information for.
	 */
	private void setItemInfo(String itemName) {
		String newline = System.getProperty("line.separator");
		int num = this.matchingItems.size();
		Item temp = null;
		for (int n = 0; n < num; n++) {
			if (this.matchingItems.get(n).getName() == itemName) {
				temp = this.matchingItems.get(n);
			}
		}
		String display = "Book: " + temp.getName() + newline +
			"Type: " + temp.getType() + newline +
			"Author: " + temp.getCreator() + newline +
			"Publisher: " + temp.getPublisher() + newline +
			"Year: " + Integer.toString(temp.getYear()) + newline +
			"Description: " + temp.getDescription() + newline +
			"Tags: ";
		LinkedList<String> tags = temp.getProperties();
		num = tags.size();
		for (int n = 0; n < num; n++) {
			display = display + tags.get(n) + ", ";
		}
		int endPoint = display.length() - 2;
		display = display.substring(0, endPoint);
		this.itemTextArea.setText(display);
	}
	
	/**
	 * Runs a search on a new set of tags when called.
	 */
	private void runSearch() {
		this.searchTags = new LinkedList<String>();
		boolean clearOne = this.tagTextField1.getText().equals("");
		boolean clearTwo = this.tagTextField2.getText().equals("");
		boolean clearThree = this.tagTextField3.getText().equals("");
		if (clearOne == false) {
			this.searchTags.add(this.tagTextField1.getText());
		}
		if (clearTwo == false) {
			this.searchTags.add(this.tagTextField2.getText());
		}
		if (clearThree == false) {
			this.searchTags.add(this.tagTextField3.getText());
		}
		this.tagTextField1.setText("");
		this.tagTextField2.setText("");
		this.tagTextField3.setText("");
		String newline = System.getProperty("line.separator");
		Profile currentProfile = new Profile("userTest", "New Search", this.searchTags);
		String display = "New Search" + newline + "Tags: ";
		int num = currentProfile.getTags().size();
		String tag;
		for (int n = 0; n < num; n++) {
			tag = currentProfile.getTags().get(n);
			display = display + tag + ", ";
		}
		int endPoint = display.length() - 2;
		display = display.substring(0, endPoint);
		profileTextArea.setText(display);
		this.matchingItems = this.mc.getItems(currentProfile);
		String itemName;
		num = this.matchingItems.size();
		this.itemComboBox.removeAllItems();
		if (num != 0) {
			for (int m = 0; m < num; m++) {
				itemName = this.matchingItems.get(m).getName();
				this.itemComboBox.addItem(itemName);
			}
		}
		else {
			this.itemTextArea.setText("No returns.");
		}
		this.searchTags.clear();
	}
}
